package com.examine.action;

import com.alibaba.fastjson.JSON;
import com.examine.bean.MyApproval;
import com.examine.bean.News;
import com.examine.bean.PageResult;
import com.examine.bean.User;
import com.examine.serviceImpl.MyApprovalServiceImpl;
import com.examine.serviceImpl.NewsServiceImpl;
import com.examine.utils.DateUtil;
import com.examine.utils.StrResponse;
import com.examine.utils.UuidUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;

public class NewsJsonAction extends ActionSupport {
    private LinkedHashMap mymp;
    private NewsServiceImpl newsService;
    private News news;

    public LinkedHashMap getModelMap() {
        return mymp;
    }
    public void setModelMap(LinkedHashMap mymp) {
        this.mymp = mymp;
    }

    public String addNews(){
        news = JSON.parseObject(StrResponse.getStrResponse(), News.class);

        newsService = new NewsServiceImpl();

        news.setId(UuidUtil.getUUID());
        news.setCreateTime(DateUtil.getCurrentTime());

        int count = newsService.insertSelective(news);
        if (count == 0){
            mymp = StrResponse.error("添加新闻资讯失败");
            return ERROR;
        }

        mymp = StrResponse.success("添加新闻资讯成功");
        return SUCCESS;
    }

    public String deletNwes(){
        news = JSON.parseObject(StrResponse.getStrResponse(), News.class);

        if(StringUtils.isEmpty(news.getId())){
            mymp = StrResponse.error("新闻ID不能为空");
            return ERROR;
        }

        newsService = new NewsServiceImpl();

        int count = newsService.deleteByPrimaryKey(news.getId());
        if (count == 0){
            mymp = StrResponse.error("删除新闻资讯失败");
            return ERROR;
        }

        mymp = StrResponse.success("删除新闻资讯成功");
        return SUCCESS;
    }

    public String selectStripNews(){
        news = JSON.parseObject(StrResponse.getStrResponse(), News.class);

        if(StringUtils.isEmpty(news.getId())){
            mymp = StrResponse.error("新闻ID不能为空");
            return ERROR;
        }

        newsService = new NewsServiceImpl();

        News news1 = newsService.selectByPrimaryKey(news.getId());
        if (news1 == null){
            mymp = StrResponse.error("查询新闻资讯失败");
            return ERROR;
        }

        mymp = StrResponse.success("查询新闻资讯成功", news1);
        return SUCCESS;
    }

    public String updateNews(){
        news = JSON.parseObject(StrResponse.getStrResponse(), News.class);

        if(StringUtils.isEmpty(news.getId())){
            mymp = StrResponse.error("新闻ID不能为空");
            return ERROR;
        }

        newsService = new NewsServiceImpl();

        news.setUpdateTime(DateUtil.getCurrentTime());

        int count = newsService.updateByPrimaryKeySelective(news);
        if (count == 0){
            mymp = StrResponse.error("修改新闻资讯失败");
            return ERROR;
        }

        mymp = StrResponse.success("修改新闻资讯成功");
        return SUCCESS;
    }

    public String selectNewsAll(){
        news = JSON.parseObject(StrResponse.getStrResponse(), News.class);

        newsService = new NewsServiceImpl();

        List<News> newsList = newsService.getAllNewsList(news);
        if (newsList.size() == 0){
            mymp = StrResponse.error("查询所有新闻资讯失败");
            return ERROR;
        }

        int countLike = newsService.queryCountLike(news);

        PageResult pageResult = new PageResult();

        pageResult.setCurrentPage(news.getPageResult().getCurrentPage());
        pageResult.setPageSize(news.getPageResult().getPageSize());
        pageResult.setTotalSize(countLike);
        pageResult.setItems(newsList);

        mymp = StrResponse.success("查询新闻资讯成功", pageResult);
        return SUCCESS;
    }


}
